<template>
	<view>
		<view style="padding: 0 20upx;background-color: white;">
			<view style="display: flex;flex-direction: row;align-items: center;padding: 15upx 0;">
				<view style="margin-right: 30upx;flex-direction: row;height: 60upx;display: flex;
				align-items: center;border-radius: 30upx;border: 3upx solid #F0F0F0;padding-right: 20upx;">
					<image style="width: 60upx;height: 60upx;border-radius: 30upx;border: 3upx solid #F0F0F0;" src="../../../static/tabbar/mine01.png"></image>
					<text style="margin-left: 10upx;font-size: 30upx;font-weight: bold;">{{current_team.teamName}}</text>
				</view>
				<view @tap="toSearch" style="height: 60upx;flex: 1;background-color: #F0F0F0;text-align: center;margin-left: 30upx;border-radius: 30upx;display: flex;align-items: center;justify-content: center;">
					<image src="../../../static/tabbar/cart02.png" style="width: 30upx;height: 30upx;"></image>
					<text style="font-size: 30upx;margin-left: 10upx;color: #A8A8A8;">搜索商品</text>
				</view>
			</view>

			<!-- 轮播图 -->
			<swiper indicator-dots autoplay indicator-active-color="white" style="height: 200upx;">
				<swiper-item v-for="i in 3" :key="i">
					<image style="width: 100%;height: 200upx;border-radius: 15upx;" src="../../../static/5c331ec8ad0767c2f78171ffa13c2ee0.jpg"></image>
				</swiper-item>
			</swiper>
			<!-- 分类 -->
			<swiper style="height: 380upx;" indicator-dots indicator-active-color="#FF387B">
				<swiper-item v-for="(group,k) in category" :key="k">
					<view style="width: 100%;height: 340upx;padding: 20upx 0 20upx 0;">
						<view @tap="toCategory(10*k+i)" v-for="(item,i) in group" :key="i" style="width: 142upx;height: 160upx;display: flex;align-items: center;flex-direction: column;float: left;justify-content: center;">
							<image style="width: 80upx;height: 80upx;border-radius: 40upx;" :src="item.iconUrl"></image>
							<text style="font-size: 25upx;color: #666666;">{{item.name}}</text>
						</view>
					</view>
				</swiper-item>
			</swiper>
		</view>
		<!-- 商品列表 -->
		<view style="padding: 20upx;">
			<view v-for="(item,k) in goods.list" :key="k" style="background-color: white;border-radius: 15upx;margin-bottom: 20upx;">
				<image @tap="detail(item.goodsId)" style="width: 100%;height: 360upx;border-radius: 15upx;" :src="item.picUrl"></image>
				<view style="padding: 10upx 20upx;">
					<view @tap="detail(item.goodsId)" style="font-size: 35upx;font-weight: bold;">{{item.name}}</view>
					<view @tap="detail(item.goodsId)" style="flex-direction: row;font-size: 25upx;line-height: 60upx;">
						<text style="background-color: #FF541B;color: white;padding: 5upx 20upx;border-radius: 20upx;">已抢{{item.sales}}件</text>
						<text style="border: 1upx solid #F0F0F0;padding: 5upx 20upx;margin-left: 10upx;border-radius: 10upx;">预计<text
							 style="color: #F55746;">{{item.deliveryTime}}</text>配送</text>
					</view>
					<view style="display: flex;flex-direction: row;justify-content: space-between;">
						<view>
							<text style="font-size: 45upx;color: #FB206A;font-weight: bold;">￥{{item.favorablePrice}}</text>
							<text style="font-size: 25upx;color: #BBBBBB;">￥{{item.retailPrice}}</text>
						</view>
						<view style="font-weight: bold;display: flex;align-items: center;">
							<image @tap="subtract(item)" style="width: 45upx;height: 45upx;" :src="select[item.goodsId] > 0 ? '/static/subtract.png' : '/static/subtract01.png'"></image>
							<!-- <text style="margin: 20upx;font-size: 35upx;">{{select[item.goodsId]}}</text> -->
							<text style="margin: 20upx;font-size: 35upx;">{{select[item.goodsId] ? select[item.goodsId].count : 0}}</text>
							<image @tap="add(item)" style="width: 45upx;height: 45upx;" src="/static/add.png"></image>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				flimg: 'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2472951089,4139669030&fm=26&gp=0.jpg',
				current_team: null,
				category: [],
				goods: {
					noData: true,
					pageNum: 1,
					pageSize: 10,
					list: []
				},
				select: this.shopCart
			}
		},
		onLoad() {
			this.getTeam()
			this.initCart()
			setTimeout(()=>{
				this.select = this.shopCart
				this.$forceUpdate()
			},500)
			
		},
		onShow() {
			var current_team = uni.getStorageSync('current_team')
			if (current_team) {
				this.current_team = JSON.parse(current_team)
			}
		},
		onTabItemTap() {
			this.select = this.shopCart
			this.$forceUpdate()
		},
		methods: {
			toCategory(index) {
				uni.setStorageSync('categoryIndex',index)
				uni.switchTab({
					url: '../category/category'
				})
			},
			add(goods) {
				this.addCart(goods)
				this.select = this.shopCart
				this.$forceUpdate()
			},
			subtract(goods) {
				this.subtractCart(goods)
				this.select = this.shopCart
				this.$forceUpdate()
			},
			// 获取当前团长
			getTeam() {
				var current_team = uni.getStorageSync('current_team')
				if (current_team) {
					this.current_team = JSON.parse(current_team)
					this.getCategory()
					this.getGoods()
				} else {
					uni.navigateTo({
						url: '../../launch/select_community'
					})
				}
			},
			getCategory() {
				uni.request({
					url: this.apiUrl + "/client/category/list",
					method: "POST",
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					success: (res) => {
						const result = res.data;
						console.log(result)
						if (result.code === 200) {
							const data = result.data.list;
							for (var i = 0; i < data.length; i += 10) {
								this.category.push(data.slice(i, i + 10))
							}
							// this.category = data;
							console.log(this.category)
						} else {
							uni.showToast({
								title: result.msg,
								icon: "none"
							});
						}
					},
					fail: (res) => {
						uni.showToast({
							title: res.errMsg,
							icon: "none"
						});
					}
				})
			},
			getGoods() {
				uni.request({
					url: this.apiUrl + "/client/goods/list",
					method: "POST",
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					data: {
						// teamId: this.current_team.teamId,
						pageNum: this.goods.pageNum,
						pageSize: this.goods.pageSize
					},
					success: (res) => {
						const result = res.data;
						console.log(result)
						if (result.code === 200) {
							const data = result.data.list;

							if (this.goods.pageNum == 1) {
								this.goods.list = data;
							} else {
								for (var i = 0; i < data.length; i++) {
									this.goods.list.push(data[i])
								}
							}

							// for (var i = 0; i < data.length; i++) {
							// 	this.select[data[i].goodsId] = 0
							// }

							// console.log(1111, this.select)
						} else {
							uni.showToast({
								title: result.msg,
								icon: "none"
							});
						}
					},
					fail: (res) => {
						uni.showToast({
							title: res.errMsg,
							icon: "none"
						});
					}
				})
			},
			detail(goodsId) {
				uni.navigateTo({
					url: "../../products/detail?goodsId=" + goodsId
				})
			},
			toSearch(){
				uni.navigateTo({
					url:'../../others/search'
				})
			}
		}
	}
</script>

<style>

</style>
